<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0%;
        }

        body > div {
            width: 50%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background: green;*/
            float: left;
            position: relative;
        }

        body > div > textarea {
            width: 100%;
            height: 100%;
        }

        .float {
            position: absolute;
            right: 0;
            top: 10px
        }
    </style>
    <script>
        var RecoveredList = {};
        var commentList = {}
        function parseXml(xml,path) {
            var string = "";
            string += "type ";
            string += xml.localName.startWithUpcase() + " struct{\n"
            path = path+xml.localName+"."
            if (xml.attributes.length > 0) {
                var attributes = xml.attributes;
                for (var i = 0; i < attributes.length; i++) {
                    string += "\t" + xml.attributes[i].localName.startWithUpcase() + " string `xml:\"" + xml.attributes[i].localName + ",attr\"`\n";
                }
            }

            var toAddString = "";
            var children = xml.children;
            if (children && children.length == 0) {
                string += "\tValue string `xml:\",chardata\"`\n";
            }
            var childStringList = {}
            for (var i = 0; children && i < children.length; i++) {
                var childXml = children.item(i);
                var child_string = undefined;
                if (childXml.attributes.length > 0 || childXml.children.length > 0) {
                    child_string = "\t" + childXml.localName.startWithUpcase() + " []" + childXml.localName.startWithUpcase() + " `xml:\"" + childXml.localName + "\"`\n";
                    toAddString += parseXml(childXml,path);
                } else {
                    child_string = "\t" + childXml.localName.startWithUpcase() + " []string `xml:\"" + childXml.localName + "\"`\n"
                }
                childStringList[child_string] = child_string.replace("[]", child_string in childStringList ? "[]" : "");
            }
            for (var child_string in childStringList) {
                string += childStringList[child_string];
            }
            string += "}\n"
            console.log(path + "\n" + string);
            if (RecoveredList[xml.localName]) {
                if (RecoveredList[xml.localName] == string) {
                    return "";
                } else {
                    var contains_before = false;
                    var i = 0;
                    for (; (xml.localName + i) in RecoveredList; i++) {
                        if (RecoveredList[xml.localName + i] == string) {
                            contains_before = true;
                            break;
                        }
                    }
                    if (!contains_before) {
                        RecoveredList[xml.localName + i] = string
                    }
                }
            } else {
                RecoveredList[xml.localName] = string;
                commentList[xml.localName] = path;
            }
            return string;
        }

        String.prototype.startWithUpcase = function () {
            return this[0].toUpperCase() + this.substring(1, this.length);
        };

        function getOutputStruct() {
            var inputTextarea = document.getElementById("input");
            var outputTextarea = document.getElementById("output");
            var inputXml = (new DOMParser()).parseFromString(inputTextarea.value, "text/xml");
            inputXml = inputXml.firstChild;
            RecoveredList = {};
            parseXml(inputXml,".")
            var result_string = "";
            for (var localName in RecoveredList ){
                result_string += RecoveredList[localName];
            }
            outputTextarea.value = result_string;
        }
        window.onload = function () {
            document.getElementById("submit").onclick = getOutputStruct;
        };
    </script>
</head>
<body>
<div>
    <textarea id="input"><xml test='1'></xml></textarea>
    <button id="submit" class="float">Submit</button>
</div>
<div>
    <textarea id="output"></textarea>
</div>
</body>
</html>